import 'package:flutter_gen/gen_l10n/demo_localizations.dart';
import 'package:flutter/material.dart';
import 'package:github_client_app/env/env.dart';
import 'package:github_client_app/models/user.dart';
import 'package:github_client_app/page/home/refresh_widget.dart';
import 'package:github_client_app/provider/locale_provider.dart';
import 'package:github_client_app/router/navigator_util.dart';
import 'package:github_client_app/router/routes.dart';
import 'package:github_client_app/services/http/dio_util.dart';
import 'package:github_client_app/services/tcp/generated/disnode.pb.dart';
import 'package:github_client_app/services/tcp/tcp_util.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return RefreshWidget(
      keepScrollOffset: true,
      loadingWidget: Container(
        height: 30,
        width: 100,
        color: Colors.amber,
        alignment: Alignment.center,
        child: const Text('正在加载...'),
      ),
      onRefresh: () async {
        await Future.delayed(Duration(seconds: 2));
      },
      child: Center(
        child: SingleChildScrollView(
          child: Column(children: [
            Text(Localizations.localeOf(context).toString()),
            Text(DemoLocalizations.of(context)!.helloWorld),
            Text(DemoLocalizations.of(context)!.dataFormat(DateTime.now())),
            Consumer<LocaleProvider>(
                builder: (_, LocaleProvider localeProvider, __) {
              return ElevatedButton(
                  onPressed: () {
                    localeProvider.changeLanguage('zhHant');
                  },
                  child: const Text('设置繁体'));
            }),
            Text(Env.envConfig.envName),
            Text(Env.envConfig.envHttpUrl),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.dbpage);
                },
                child: const Text('数据库')),
            ElevatedButton(
                onPressed: () {
                  TcpUtil.pushTask(MessagePushTask(
                      jobId: "test111",
                      jobType: JobType.valueOf(24),
                      data: "testtesttest"));
                },
                child: const Text('发送推送消息')),
            ElevatedButton(
                onPressed: () {
                  TcpUtil.register();
                },
                child: const Text('发送注册消息')),
            ElevatedButton(
                onPressed: () {
                  TcpUtil.deRegister();
                },
                child: const Text('发送注销消息')),
            ElevatedButton(
                onPressed: () {
                  TcpUtil.acceptTask(MessageAcceptTask(accept: true));
                },
                child: const Text('发送接收消息')),
            ElevatedButton(
                onPressed: () {
                  TcpUtil.socketClose();
                },
                child: const Text('关闭tcp')),
            ElevatedButton(
                onPressed: () {
                  DioUtil().get(url: '/hello').then((value) {
                    User u = User.fromJson(value);
                    // print(u.name);
                  });
                },
                child: const Text('发送http请求')),
            ElevatedButton(
                onPressed: () {
                  DioUtil().get(url: '/pushy', loading: true).then((value) {
                    // User u = User.fromJson(value);
                    // // print(u.name);
                    // print(value);
                  });
                },
                child: const Text('发送http2请求')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.p5Page);
                },
                child: Text('p5page')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.breathePage);
                },
                child: Text('478呼吸小动画')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.rotatePage);
                },
                child: Text('rotatePage')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.snowPage);
                },
                child: Text('雪花')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.numCounter);
                },
                child: Text('数字计数器')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.book);
                },
                child: Text('book')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.testPage);
                },
                child: Text('test')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.home1);
                },
                child: Text('home1')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.game);
                },
                child: Text('数字小游戏')),
            ElevatedButton(
                onPressed: () {
                  NavigatorUtil.push(context, Routes.keyPage);
                },
                child: Text('key小游戏')),
            //   SizedBox(
            //     height: 200,
            //     child: FutureBuilder(
            //         future: DioUtil().get(url: '/api/articles'),
            //         builder: (context, snapshot) {
            //           if (snapshot.hasData) {
            //             // print(snapshot);
            //             var data = snapshot.data;
            //             // print(data);
            //             return SizedBox(
            //               height: 200,
            //               child: Swiper(
            //                 itemCount: data.length,
            //                 pagination: const SwiperPagination(),
            //                 control: const SwiperControl(),
            //                 itemBuilder: (context, index) {
            //                   return Container(
            //                     width: 200,
            //                     height: 200,
            //                     color: Colors.red,
            //                     child: Text(data[index]['title']),
            //                   );
            //                 },
            //               ),
            //             );
            //           } else {
            //             return const Text("no data");
            //           }
            //         }),
            //   )
          ]),
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
